/**
 * 
 */
package com.noisyair.android.alvin.activities;

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

import com.noisyair.android.alvin.Talker;
import com.noisyair.android.alvin.views.AlvinTextView;

/**
 * @author darren
 *
 */
public class AlvinListActivity extends ListActivity {

	private View mSelectedView = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Talker.init(this);
		
	}

	@Override
	public boolean onTrackballEvent(MotionEvent event) {
		try{
			
			/*
			 * There seems to be a bug in the Android code here.  When the scroll wheel moves to a newly 
			 * selected list item, the current and selected seem to fall out of sync.  i.e. The highlighted
			 * row doesn't always show up as the selected row in code... no fixes for this yet....
			 */
			//Log.i("Alvin", "cur: " + getListView().getSelectedView().toString());
			//Log.i("Alvin", "sel: " + selectedView.toString());
			
			View currentView = getListView().getSelectedView();
			if(currentView == mSelectedView){
				return super.onTrackballEvent(event);
			}
			else{
				// Set the old selected view back:													
				 ((AlvinTextView)((LinearLayout)mSelectedView).getChildAt(1)).makeViewLarge();
								
				// Update the selectView member:
				mSelectedView = currentView;
				((AlvinTextView)((LinearLayout)mSelectedView).getChildAt(1)).makeViewLargerOnFocus();
			}
		}
		catch(Exception e){
			Log.i("Alvin", "Exception thrown when moving the track ball " + e.getMessage());
			mSelectedView = getListView().getSelectedView();
		}
				
		return super.onTrackballEvent(event);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_CALL){
			Talker.setShouldTalk(true);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_CALL){
			Talker.setShouldTalk(false);
			return true;
		}
		return super.onKeyUp(keyCode, event);
	}
	
}
